//
//  FMSLib.m
//  FMS
//
//  Created by Steven on 11/2/13.
//  Copyright (c) 2013 QGSVN. All rights reserved.
//

#import "FMSLib.h"

@implementation FMSLib

+ (FMSLib *)shareClient{
    static dispatch_once_t onceToken;
    static FMSLib *client = nil;
    dispatch_once(&onceToken, ^{
        client = [[FMSLib alloc] init];
    });
    return client;
}

#pragma mark Server Methods
- (void)getDomainInfo:(NSString *)virtualHost successs:(void (^)(NSMutableDictionary *response))successs failure:(void (^)(NSError *error))failure{
    [[FMSServerAPI shareClient] getDomainInfo:virtualHost successs:^(id response){
        if ([response isKindOfClass:[NSMutableDictionary class]]) {
            NSMutableDictionary *dict = (NSMutableDictionary *)response;
            NSMutableDictionary *dictResult = [dict objectForKey:@"response"];
            successs(dictResult);
        }
        else{
            NSString * errorDesc = @"Invalid response data";
            NSDictionary * errorDict = [NSDictionary dictionaryWithObjectsAndKeys:@"invalid request",@"title",
                                        errorDesc,@"description",nil];
            NSError * error = [NSError errorWithDomain:kFMSServerDomain code:0 userInfo:errorDict];
            failure(error);
        }
    }failure:^(NSError *error){
        failure(error);
    }];
}
- (void)getCompanyId:(NSString *)virtualHost successs:(void (^)(NSMutableDictionary* response))successs failure:(void (^)(NSError *error))failure{
    [[FMSServerAPI shareClient] getCompanyId:virtualHost successs:^(id response){
        if ([response isKindOfClass:[NSMutableDictionary class]]) {
            NSMutableDictionary *dict = (NSMutableDictionary *)response;
            successs(dict);
        }
        else{
            NSString * errorDesc = @"Invalid response data";
            NSDictionary * errorDict = [NSDictionary dictionaryWithObjectsAndKeys:@"invalid request",@"title",
                                        errorDesc,@"description",nil];
            NSError * error = [NSError errorWithDomain:kFMSServerDomain code:0 userInfo:errorDict];
            failure(error);
        }
    }failure:^(NSError *error){
        failure(error);
    }];
}

- (void)getTimeDurationSuccess:(void (^)(NSMutableDictionary *response))successs failure:(void (^)(NSError *error))failure{
    [[FMSServerAPI shareClient] getTimeDurationSuccess:^(id response){
        if ([response isKindOfClass:[NSMutableDictionary class]]) {
            NSMutableDictionary *dict = (NSMutableDictionary *)response;
            if ([[[dict objectForKey:@"meta"] objectForKey:@"code"] intValue] == 200 && [[dict objectForKey:@"response"] isKindOfClass:[NSMutableDictionary class]]) {
                successs([dict objectForKey:@"response"]);
            }
            else{
                NSString * errorDesc = @"Invalid response data";
                NSDictionary * errorDict = [NSDictionary dictionaryWithObjectsAndKeys:@"invalid request",@"title",
                                            errorDesc,@"description",nil];
                NSError * error = [NSError errorWithDomain:kFMSServerDomain code:0 userInfo:errorDict];
                failure(error);
            }
        }
        else{
            NSString * errorDesc = @"Invalid response data";
            NSDictionary * errorDict = [NSDictionary dictionaryWithObjectsAndKeys:@"invalid request",@"title",
                                        errorDesc,@"description",nil];
            NSError * error = [NSError errorWithDomain:kFMSServerDomain code:0 userInfo:errorDict];
            failure(error);
        }
        
    }failure:^(NSError *error){
        failure(error);
    }];
}

- (void)checkPermissionUser:(NSString *)user auth:(NSString *)auth success:(void (^)(NSMutableDictionary * response))successs failure:(void (^)(NSError *error))failure{
    [[FMSServerAPI shareClient] checkPermissionUser:user auth:auth success:^(id response){
        if ([response isKindOfClass:[NSMutableDictionary class]]) {
            NSMutableDictionary *dict = (NSMutableDictionary *)response;
            if ([[[dict objectForKey:@"meta"] objectForKey:@"code"] intValue] == 200 && [[dict objectForKey:@"response"] isKindOfClass:[NSMutableDictionary class]]) {
                successs([dict objectForKey:@"response"]);
            }
            else{
                NSString * errorDesc = @"Invalid response data";
                NSDictionary * errorDict = [NSDictionary dictionaryWithObjectsAndKeys:@"invalid request",@"title",
                                            errorDesc,@"description",nil];
                NSError * error = [NSError errorWithDomain:kFMSServerDomain code:0 userInfo:errorDict];
                failure(error);
            }
        }
        else{
            NSString * errorDesc = @"Invalid response data";
            NSDictionary * errorDict = [NSDictionary dictionaryWithObjectsAndKeys:@"invalid request",@"title",
                                        errorDesc,@"description",nil];
            NSError * error = [NSError errorWithDomain:kFMSServerDomain code:0 userInfo:errorDict];
            failure(error);
        }
        
    }failure:^(NSError *error){
        failure(error);
    }];
}

#pragma mark User Methods
//get user account information
- (void)loginWithUser:(NSString *)userEmail password:(NSString *)password successs:(void (^)(NSMutableDictionary *response))successs failure:(void (^)(NSError *error))failure{
    [[FMSServerAPI shareClient] loginWithUser:userEmail password:password successs:^(id response){
        if ([response isKindOfClass:[NSMutableDictionary class]]) {
            NSMutableDictionary *dict = (NSMutableDictionary *)response;
            if ([[[dict objectForKey:@"meta"] objectForKey:@"code"] intValue] == 200 && [[dict objectForKey:@"response"] isKindOfClass:[NSMutableDictionary class]]) {
                NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                NSString *role = [[dict objectForKey:@"response"] objectForKey:@"role"];
                
                if (role != nil) {
                    [userDefaults setObject:role forKey:USER_ROLE];
                }
                
                successs([dict objectForKey:@"response"]);
            }
            else{
                NSString * errorDesc = @"Invalid response data";
                NSDictionary * errorDict = [NSDictionary dictionaryWithObjectsAndKeys:@"invalid request",@"title",
                                            errorDesc,@"description",nil];
                NSError * error = [NSError errorWithDomain:kFMSServerDomain code:0 userInfo:errorDict];
                failure(error);
            }
        }
        else{
            NSString * errorDesc = @"Invalid response data";
            NSDictionary * errorDict = [NSDictionary dictionaryWithObjectsAndKeys:@"invalid request",@"title",
                                        errorDesc,@"description",nil];
            NSError * error = [NSError errorWithDomain:kFMSServerDomain code:0 userInfo:errorDict];
            failure(error);
        }
        
    }failure:^(NSError *error){
        failure(error);
    }];
}

- (void)forgotPasswordEmail:(NSString *)userEmail successs:(void (^)(NSMutableDictionary *response))successs failure:(void (^)(NSError *error))failure{
    [[FMSServerAPI shareClient] forgotPasswordEmail:userEmail successs:^(id response){
        if ([response isKindOfClass:[NSMutableDictionary class]]) {
            NSMutableDictionary *dict = (NSMutableDictionary *)response;
            if ([[[dict objectForKey:@"meta"] objectForKey:@"code"] intValue] == 200 && [[dict objectForKey:@"response"] isKindOfClass:[NSMutableDictionary class]]) {
                NSMutableDictionary *response = (NSMutableDictionary *)[dict objectForKey:@"response"];
                if ([[response objectForKey:@"status"] isEqualToString:@"Fail"]) {
                    
                    NSString * errorDesc = [response objectForKey:@"info"];
                    NSDictionary * errorDict = [NSDictionary dictionaryWithObjectsAndKeys:@"Failed to forgot password",@"title",
                                                errorDesc,@"description",nil];
                    NSError * error = [NSError errorWithDomain:kFMSServerDomain code:0 userInfo:errorDict];
                    failure(error);
                }
                else{
                    successs(response);
                }
            }
            else{
                NSString * errorDesc = @"Failed to forgot password";
                NSDictionary * errorDict = [NSDictionary dictionaryWithObjectsAndKeys:@"invalid request",@"title",
                                            errorDesc,@"description",nil];
                NSError * error = [NSError errorWithDomain:kFMSServerDomain code:0 userInfo:errorDict];
                failure(error);
            }
        }
        else{
            NSString * errorDesc = @"Failed to connect to server";
            NSDictionary * errorDict = [NSDictionary dictionaryWithObjectsAndKeys:@"invalid request",@"title",
                                        errorDesc,@"description",nil];
            NSError * error = [NSError errorWithDomain:kFMSServerDomain code:0 userInfo:errorDict];
            failure(error);
        }
        
    }failure:^(NSError *error){
        NSString * errorDesc = @"A network connection is required to sent request forgot password.Please check connection status.";
        NSDictionary * errorDict = [NSDictionary dictionaryWithObjectsAndKeys:@"invalid request",@"title",
                                    errorDesc,@"description",nil];
        error = [NSError errorWithDomain:kFMSServerDomain code:0 userInfo:errorDict];
        
        failure(error);
    }];
}

//get user information by email from server
- (void)getUserInfoAuthenticationUserEmail:(NSString *)userEmail password:(NSString *)password fromCompnay:(int)companyId success:(void (^)(NSMutableDictionary *response))successs failure:(void (^)(NSError *error))failure{
    
    [[FMSServerAPI shareClient] getUserInfoAuthenticationUserEmail:userEmail password:password fromCompnay:companyId success:^(id response){
        if ([response isKindOfClass:[NSMutableDictionary class]]) {
            NSMutableDictionary *dict = (NSMutableDictionary *)response;
            if ([dict isKindOfClass:[NSMutableDictionary class]]) {
                
                NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                if([[userDefaults objectForKey:@"ownerID"] intValue]>0){
                    [userDefaults setObject:[dict objectForKey:@"userId"] forKey:@"ownerID"];
                    [userDefaults setObject:@"ok" forKey:@"account"];
                }
                
                successs(dict);
            }
            else{
                NSString * errorDesc = @"Invalid response data";
                NSDictionary * errorDict = [NSDictionary dictionaryWithObjectsAndKeys:@"invalid request",@"title",
                                            errorDesc,@"description",nil];
                NSError * error = [NSError errorWithDomain:kFMSServerDomain code:0 userInfo:errorDict];
                failure(error);
            }
        }
        else{
            NSString * errorDesc = @"Invalid response data";
            NSDictionary * errorDict = [NSDictionary dictionaryWithObjectsAndKeys:@"invalid request",@"title",
                                        errorDesc,@"description",nil];
            NSError * error = [NSError errorWithDomain:kFMSServerDomain code:0 userInfo:errorDict];
            failure(error);
        }
        
    }failure:^(NSError *error){
        failure(error);
    }];
}

- (void)getOwnerUserInfoByEmail:(NSString *)email success:(void (^)(NSMutableDictionary *response))successs failure:(void (^)(NSError *error))failure{
    [[FMSServerAPI shareClient] getOwnerUserInfoByEmail:email success:^(id response){
        
        if ([response isKindOfClass:[NSMutableDictionary class]]) {
            NSMutableDictionary *dict = (NSMutableDictionary *)response;
            if ([dict isKindOfClass:[NSMutableDictionary class]] && [[dict allKeys] count] > 0) {
                successs(dict);
            }
            else{
                NSString * errorDesc = @"Invalid response data";
                NSDictionary * errorDict = [NSDictionary dictionaryWithObjectsAndKeys:@"invalid request",@"title",
                                            errorDesc,@"description",nil];
                NSError * error = [NSError errorWithDomain:kFMSServerDomain code:0 userInfo:errorDict];
                failure(error);
            }
        }
        else{
            NSString * errorDesc = @"Invalid response data";
            NSDictionary * errorDict = [NSDictionary dictionaryWithObjectsAndKeys:@"invalid request",@"title",
                                        errorDesc,@"description",nil];
            NSError * error = [NSError errorWithDomain:kFMSServerDomain code:0 userInfo:errorDict];
            failure(error);
        }

        
    }failure:^(NSError *error){
        failure(error);
    }];
}
@end
